package com.jiyun.controller;

import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.jiyun.feign.CouserFeng;
//import com.jiyun.feign.JsNumfeng;
import com.jiyun.feign.FindPxNumFeng;
import com.jiyun.feign.RollFeng;
import com.jiyun.model.day08.Course;
import com.jiyun.model.day11.Roll;
import com.jiyun.model.day12.Rival;
import com.jiyun.model.day14.Member;
import com.jiyun.model.day14.RivalMember;
import com.jiyun.model.day15.TraineeCourse;
import com.jiyun.model.day15.TraineeMember;
import com.jiyun.service.MemberService;
import com.jiyun.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

import java.util.List;

/**
 * @Title: ${王坤}
 * @Author itmei
 * @Date 2023/8/22 18:38
 */
@RestController
@RequestMapping("member")
public class MemberController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CouserFeng couserFeng;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private FindPxNumFeng findPxNumFeng;

    @Autowired
    private RollFeng rollFeng;

    @RequestMapping("login")
    public Result login(@RequestBody Member member) {
        try {
            String token = memberService.login(member);
            if (token != null)
                return Result.success(token);
            return Result.error();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("findKcNum")
    public Result findKcNum(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            String o = (String) redisTemplate.opsForValue().get(token);
            Member member = JSONUtil.toBean(o, Member.class);
            List<TraineeMember> kcNum = couserFeng.findKcNum();
            int num = 0;
            for (TraineeMember traineeCourse : kcNum) {
                if (traineeCourse.getMemberId() == member.getId()) {
                    num++;
                }
            }
            return Result.success(num);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("findzxKC")
    public Result findzxKC(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            String o = (String) redisTemplate.opsForValue().get(token);
            Member member = JSONUtil.toBean(o, Member.class);
            List<TraineeMember> kcNum = couserFeng.findKcNum();
            int num = 0;
            for (int i = 0; i < kcNum.size(); i++) {
                for (TraineeMember traineeCourse : kcNum) {
                    if (traineeCourse.getMemberId() == member.getId()) {
                        num++;
                    }
                }
            }
            return Result.success(num);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("findJsNum")
    public Result findJsNum(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            String o = (String) redisTemplate.opsForValue().get(token);
            Member member = JSONUtil.toBean(o, Member.class);
            List<TraineeCourse> kcNum = couserFeng.findJsNum();
            int num = 0;
            for (TraineeCourse traineeCourse : kcNum) {
                if (traineeCourse.getTraineeId() == member.getId()) {
                    num++;
                }
            }
            return Result.success(num);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("findzxKC2")
    public Result findzxKC2(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            String o = (String) redisTemplate.opsForValue().get(token);
            Member member = JSONUtil.toBean(o, Member.class);
            List<TraineeCourse> kcNum = couserFeng.findJsNum();
            int num = 0;
            for (TraineeCourse traineeCourse : kcNum) {
                if (traineeCourse.getTraineeId() == member.getId()) {
                    num++;
                }
            }
            return Result.success(num);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("findzxKC3")
    public Result findzxKC3(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            String o = (String) redisTemplate.opsForValue().get(token);
//            Member member = JSONUtil.toBean(o, Member.class);
            List<TraineeCourse> kcNum = couserFeng.findJsNum();
            int num = 0;
            for (TraineeCourse traineeCourse : kcNum) {
//                if (traineeCourse.getTraineeId()==member.getId()){
//                    num++;
//                }
                if (traineeCourse.getCourseId() != null) {
                    num++;
                }
            }
            return Result.success(num);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("findPxNum")
    public Result findPxNum(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            String o = (String) redisTemplate.opsForValue().get(token);
            Member member = JSONUtil.toBean(o, Member.class);
            List<Rival> kcNum = findPxNumFeng.findPX3();
            int num = 0;
            for (Rival rival : kcNum) {
                if (rival != null) {
                    num++;
                }
            }
            return Result.success(num);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("findzxPX")
    public Result findzxPX(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            String o = (String) redisTemplate.opsForValue().get(token);
            Member member = JSONUtil.toBean(o, Member.class);
            List<RivalMember> kcNum = findPxNumFeng.findPxNum();
            int num = 0;

            for (RivalMember traineeCourse : kcNum) {
                if (traineeCourse.getMemberId() == member.getId()) {
                    num++;
                }
            }

            return Result.success(num);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("findzxPX2")
    public Result findzxPX2(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            String o = (String) redisTemplate.opsForValue().get(token);
//            Member member = JSONUtil.toBean(o, Member.class);
            List<RivalMember> kcNum = findPxNumFeng.findPxNum();
            int num = 0;
            for (RivalMember traineeCourse : kcNum) {
                if (traineeCourse.getMemberId() != null) {
                    num++;
                }
            }
            return Result.success(num);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("findzxPX3")
    public Result findzxPX3(HttpServletRequest request) {
        try {
            String token = request.getHeader("token");
            String o = (String) redisTemplate.opsForValue().get(token);
//            Member member = JSONUtil.toBean(o, Member.class);
            List<Rival> kcNum = findPxNumFeng.findPX3();
            int num = 0;

            for (Rival r : kcNum) {
//                if (traineeCourse.getRivalId() == member.getId()) {
//                    num++;
//                }
                if (r.getId() != null) {
                    num++;
                }
            }
            return Result.success(num);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }

    @RequestMapping("findzxJC")
    public Result findzxJC() {
        try {
            List<Roll> roll = rollFeng.findRoll();
            int num = 0;
            for (Roll roll1 : roll) {
                if (roll1.getTid() == 1) {
                    num++;
                }
            }
            return Result.success(num);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("findzxJC2")
    public Result findzxJC2() {
        try {
            List<Roll> roll = rollFeng.findRoll();
            int num = 0;
            for (Roll roll1 : roll) {
                if (roll1.getTid() == 2) {
                    num++;
                }
            }
            return Result.success(num);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("findzxJC3")
    public Result findzxJC3() {
        try {
            List<Roll> roll = rollFeng.findRoll();
            int num = 0;
            for (Roll roll1 : roll) {
                if (roll1.getTid() == 3) {
                    num++;
                }
            }
            return Result.success(num);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("findCS")
    public Result findCS(){
        try {
            List<TraineeCourse> jsNum = couserFeng.findJsNum();
            int num = 0;
            for (TraineeCourse traineeCourse : jsNum) {
                if (traineeCourse.getTraineeId()==1){
                    num++;
                }
            }
            return Result.success(num);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("findCS2")
    public Result findCS2(){
        try {
            List<TraineeCourse> jsNum = couserFeng.findJsNum();
            int num = 0;
            for (TraineeCourse traineeCourse : jsNum) {
                if (traineeCourse.getTraineeId()==2){
                    num++;
                }
            }
            return Result.success(num);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }
    @RequestMapping("findCS3")
    public Result findCS3(){
        try {
            List<TraineeCourse> jsNum = couserFeng.findJsNum();
            int num = 0;
            for (TraineeCourse traineeCourse : jsNum) {
                if (traineeCourse.getTraineeId()==3){
                    num++;
                }
            }
            return Result.success(num);
        }catch (Exception e){
            e.printStackTrace();
            return Result.error();
        }
    }
//    @RequestMapping("findKC")
//    public Result findKC(){
//        try {
//            List<Course> px = couserFeng.findPX();
//            int num=0;
//            for (Course course : px) {
//                if (course.getId()==1){
//                    for (int i = 0; i < course.getTid().length(); i++) {
//                        num++;
//                    }
//                }
//            }
//            return Result.success(num);
//        }catch (Exception e){
//            e.printStackTrace();
//            return Result.error();
//        }
//    }
//    @RequestMapping("findKC2")
//    public Result findKC2(){
//        try {
//            List<Course> px = couserFeng.findPX();
//            int num=0;
//            for (Course course : px) {
//                if (course.getId()==18){
//                    for (int i = 0; i < course.getTid().length(); i++) {
//                        num++;
//                    }
//                }
//            }
//            return Result.success(num);
//        }catch (Exception e){
//        e.printStackTrace();
//        return Result.error();
//    }
//    }
//    @RequestMapping("findKC3")
//    public Result findKC3(){
//        try {
//            List<Course> px = couserFeng.findPX();
//            int num=0;
//            for (Course course : px) {
//                if (course.getId()==14){
//                    for (int i = 0; i < course.getTid().length(); i++) {
//                        num++;
//                    }
//                }
//            }
//            return Result.success(num);
//        }catch (Exception e){
//            e.printStackTrace();
//            return Result.error();
//        }
//    }
}
